//
//  UIView+snapshot.m
//  KSBridgeDesign
//
//  Created by huzhaoke on 2021/4/5.
//

#import "UIView+snapshot.h"

@implementation UIView (snapshot)

- (UIImage *)ks_snapshot
{
    return [self ks_snapshotWithOpaque:YES];
}

- (UIImage *)ks_snapshotWithOpaque:(BOOL)opaque
{
    if (self.bounds.size.width > 0 && self.bounds.size.height > 0) {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, opaque, 0);
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    } else {
        return nil;
    }
}

- (UIImage *)ks_snapshotWithLayerRender
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}


@end
